package cn.edu.sgu.www.mhxysy.service.impl;

import cn.edu.sgu.www.mhxysy.base.Pager;
import cn.edu.sgu.www.mhxysy.entity.Server;
import cn.edu.sgu.www.mhxysy.mapper.ServerMapper;
import cn.edu.sgu.www.mhxysy.pager.ServerPager;
import cn.edu.sgu.www.mhxysy.service.ServerService;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author heyunlin
 * @version 1.0
 */
@Service
public class ServerServiceImpl implements ServerService {

	private final ServerMapper serverMapper;

	@Autowired
	public ServerServiceImpl(ServerMapper serverMapper) {
		this.serverMapper = serverMapper;
	}

	@Override
	public void insert(Server server) {
		server.setId(null);

		serverMapper.insert(server);
	}
	
	@Override
	public void deleteById(Integer id) {
		serverMapper.deleteById(id);
	}

	@Override
	public void updateById(Server server) {
		serverMapper.updateById(server);
	}

	@Override
	public List<Server> selectAll() {
		return serverMapper.selectList(null);
	}

	@Override
	public Server selectById(Integer id) {
		return serverMapper.selectById(id);
	}

	@Override
	public List<Server> selectByAreaId(Integer areaId) {
		QueryWrapper<Server> wrapper = new QueryWrapper<>();

		wrapper.eq("area_id", areaId);

		return serverMapper.selectList(wrapper);
	}

	@Override
	public Page<Server> selectByPage(ServerPager pager) {
		QueryWrapper<Server> wrapper = new QueryWrapper<>();
		Page<Server> page = Pager.ofPage(pager);

		wrapper.like(
				StringUtils.isNotEmpty(pager.getName()),
				"name", pager.getName()
		);
		wrapper.eq(
				pager.getAreaId() != null,
				"area_id", pager.getAreaId()
		);

		return serverMapper.selectPage(page, wrapper);
	}

}